define([],function(){
    function pageLogic(config){
      this.pageview = config.pageview;
      this.id = this.pageview.params.id;
      this.isModify = true;
      if(this.id===null||this.id===undefined){
        this.isModify = false;
      }
      this._loadData();
      this.templateData = [];
      this.curSelectedDesignerItem = null;
    }
    pageLogic.prototype = {
      _loadData:function(){
        var _this = this;
        if(this.isModify){
          this.pageview.showLoading({
            text:"正在努力加载...",
            loadingColor:"#000",
            reloadCallback:function(){
              _this.ajaxLoadData();
            }
          });

          this.ajaxLoadData();

        }
      },
      ajaxLoadData:function(){
        var _this = this;
        this.pageview.ajax({
          type:"GET",
          url:"/template/detail/"+this.id,
          data:{
          },
          success:function(data){
            if(data.code === 0 ){
              _this.loadSuccess(data);
            }
            _this.pageview.hideLoading(data.code === 0);
          },
          error:function(){
            _this.pageview.hideLoading(false);
          }
        });
      },
      loadSuccess:function(data){
        var _this = this;

        data = data.data;

        this.templateVisibleRange = data.templateVisibleRange||[];
        for(var i=0,j=this.templateVisibleRange.length;i<j;i++){
          var templateVisibleRangeItem = this.templateVisibleRange[i];
          templateVisibleRangeItem.name = templateVisibleRangeItem.userName||templateVisibleRangeItem.teamName;
        }


        this.pageview.delegate("template_wrap_name_input",function(target){
          target.setValue(data.title);
        });
        this.pageview.delegate("template_wrap_desc_input",function(target){
          target.setValue(data.describe);
        });

        try{

          var content = data.content;
          content = JSON.parse(content);
          this.pageview.delegate("dailydesigner",function(target){
              target.bindData(content);
          });
        }catch(e){
          this.pageview.showTip({text:"模板出错!"});
        }


        if(data.dataStatus===1){
          //已启用
          this.setDisabled(true);
        }else if(data.dataStatus===0){
          //未启用
          this.isDisabled = false;
        }

        this.pageview.delegate("tips_repeat",function(target){
          target.bindData(_this.templateVisibleRange);
          target.setDisabled(data.dataStatus===1,true);


        });

        _this.pageview.delegate("template_wrap_type_repeat",function(target){
            target.eachItem(function(row,index){
              row.delegate("template_wrap_item",function(target){
                 if(target.datasource.id===data.dataType){
                  target.rowInstance.select();
                }
              });


            });
        });

        // this.pageview.refs.dailydesigner.bindData(demodata.data.body);


      },

      template_wrap_range_icon_click:function(sender){
        this.pageview.refs.personSelector.show(this.templateVisibleRange);
      },
      repeat_nodata_click:function() {
        this.pageview.refs.personSelector.show(this.templateVisibleRange);
      },
      setDisabled:function(isDisabled){
        var _this = this;
        this.isDisabled = isDisabled;
        if(isDisabled===true){
          this.pageview.delegate("saveanduse_btn",function(target){
            target.addClass("displaynone");
          });
          this.pageview.delegate("save_btn",function(target){
            target.addClass("displaynone");
          });

          this.pageview.delegate("stop_btn",function(target){
            target.removeClass("displaynone");
          });


          this.pageview.delegate("repeat_nodata",function(target){
            target.setDisabled(true,true);
          });
          this.pageview.delegate("template_wrap_range_icon",function(target){
            target.setDisabled(true,true);
          });

          this.pageview.$el.addClass("disabled-editor");

          this.pageview.delegate("template_wrap",function(target){
            window.setTimeout(function(){
              target.setDisabled(true,true);
              _this.pageview.refs.dailydesigner.destroy();
            },500);
          });
        }else{
          this.pageview.delegate("saveanduse_btn",function(target){
            target.removeClass("displaynone");
          });
          this.pageview.delegate("save_btn",function(target){
            target.removeClass("displaynone");
          });

          this.pageview.delegate("stop_btn",function(target){
            target.addClass("displaynone");
          });

          this.pageview.delegate("repeat_nodata",function(target){
            target.setDisabled(false,true);
          });
          this.pageview.delegate("template_wrap_range_icon",function(target){
            target.setDisabled(false,true);
          });

          this.pageview.$el.removeClass("disabled-editor");

          this.pageview.delegate("template_wrap",function(target){
            window.setTimeout(function(){
              target.setDisabled(false,true);
              _this.pageview.refs.dailydesigner.initEvent();
            },500);
          });
        }
      },
      stop_btn_click:function(){
        var _this = this;
        this.pageview.showSubmiting({
          text:"正在处理",
          successText:"操作成功"
        });
         this.pageview.ajax({
           url:"/template/stop",
           type:"POST",
           data:{id:this.id},
           success:function(data){
             if(data.code===0){
               _this.setDisabled(false);
             }else{
             }
             _this.pageview.hideSubmiting(data.code===0);
           },error:function(e){
             _this.pageview.hideSubmiting(false);
           }
         });
      },
      //template_wrap_name_input
      _getAddParams:function(){
        var title = this.pageview.refs.template_wrap_name_input.getValue();
        if(title.length===0){
          this._showTemplateWrap();
          this.pageview.showTip({text:"请输入模板名称",duration:1000});
          return false;
        }
        if(title.length>10){
          this._showTemplateWrap();
          this.pageview.showTip({text:"模板名称过长",duration:1000});
          return false;
        }
        var describe = this.pageview.refs.template_wrap_desc_input.getValue()||"";

        if(describe.length>20){
          this._showTemplateWrap();
          this.pageview.showTip({text:"模板描述过长",duration:1000});
          return false;
        }
        var config = this.pageview.refs.dailydesigner.getConfig();

        var dateType = this.pageview.refs.template_wrap_type_repeat.selectedItems[0].datasource.id;

        var viewRange = [];
        this.templateVisibleRange = this.templateVisibleRange||[];
        for(var i=0,j=this.templateVisibleRange.length;i<j;i++){
          var itemData = this.templateVisibleRange[i];
          if(itemData.dataType===1){
             viewRange.push({dataType:1,teamId:itemData.teamId,teamName:itemData.teamName});
          }
          if(itemData.dataType===4){
             viewRange.push({dataType:4,memberId:itemData.memberId});
          }
          if(itemData.dataType===3){
             viewRange.push({dataType:3,teamId:itemData.teamId,teamName:itemData.teamName});
          }
        }
        //[{dataType:1,memberId:1234},{dataType:2,teamId:123,teamName:"asd"}] //data_type '1：部门，2：群组，3 团队，4：个人',
        var data = {
                  content: JSON.stringify(config),
                  "title": title,
                  "describe": describe,
                  "dataType": dateType,
                  visibleRange:viewRange.length===0?0:1,
                  visibleRangeStr:JSON.stringify(viewRange),
                  "source": 3
        };
        return data;
      },
      saveanduse_btn_click:function(sender,p){
        var _this = this;
        var params = this._getAddParams();
        this.save_success_text = "保存并启用成功!";
        if(this.save_success_dialog_label){
          this.save_success_dialog_label.setText(this.save_success_text);
        }
        if(params===false){
          return;
        }
        var url = "/template/addAndStart";
        if(isNaN(this.id)){
        }else{
          params.id = this.id;
          params.dataStatus = 1;
          url = "/template/edit";
        }
        this.pageview.showSubmiting({
          text:"正在处理",
          successText:null
        });
        this.pageview.ajax({
          url:url,
          type:"POST",
          data:params,
          success:function(data){
            if(data.code===0){
              _this.id = data.data.id||_this.id;
              _this.setDisabled(true);
            }else{
            }
            _this.pageview.hideSubmiting(data.code===0);
            _this.pageview.showDialog("save_success_dialog");
          },error:function(e){
            _this.pageview.hideSubmiting(false);
          }

        });


      },
      save_btn_click:function(sender,p){
        var _this = this;
        this.save_success_text = "保存成功!";
        if(this.save_success_dialog_label){
          this.save_success_dialog_label.setText(this.save_success_text);
        }
        var params = this._getAddParams();
        if(params===false){
          return;
        }
        var url  ="/template/edit";
        if(!isNaN(this.id)){
          params.id = this.id;
        }else{
          url  ="/template/add";
        }
        this.pageview.showSubmiting({
          text:"正在处理",
          successText:null,
        });
        this.pageview.ajax({
          url:url,
          type:"POST",
          data:params,
          success:function(data){
            if(data.code===0){
              _this.id = data.data.id||_this.id;
              _this.pageview.showDialog("save_success_dialog");
              _this.pageview.hideSubmiting(true);

            }else{
                _this.pageview.hideSubmiting(false,data.msg);
            }
          },error:function(e){
            _this.pageview.hideSubmiting(false);
          }
        });

      },
      template_wrap_type_repeat_didmount:function(sender){
        sender.components[0].select();
      },
      template_wrap_type_repeat_itemclick:function(sender,params){
        sender.select();
      },
      dailydesigner_itemselectted:function(sender,params){
        this.pageview.refs.segment.components[0].select();
        this._designerItemSelected();
      },
      tips_repeat_itemclick:function(sender,params){
        for(var i=0,j=this.templateVisibleRange.length;i<j;i++){
          var item = this.templateVisibleRange[i];
          if(item.dataType===1){
            if(sender.datasource.teamId === item.teamId){
              this.templateVisibleRange.splice(i,1);
              break;
            }
          }else if(item.dataType===4){
            if(sender.datasource.memberId === item.memberId){
              this.templateVisibleRange.splice(i,1);
              break;
            }
          }else if(item.dataType===3){
            if(sender.datasource.teamId === item.teamId){
              this.templateVisibleRange.splice(i,1);
              break;
            }
          }
        }
        sender.remove();
      },
      personSelector_ok:function(sender,params){
        var selectedData = sender.selectedData;
        this.templateVisibleRange = selectedData;
        this.pageview.delegate("tips_repeat",function(target){
          target.bindData(selectedData);
        });


        this.pageview.refs.personSelector.hide();
      },

      _showTemplateWrap:function(){
        this.pageview.refs.segment.components[1].select();
        this.pageview.refs.segment_statusview.changeStatus("template_wrap");
      },
      dailydesigner_afteritemdelete:function(sender,params){
        this._showTemplateWrap();
      },
      preview_btn_click:function(sender,params){
        this.pageview.showPage({pageKey:"preview",mode:"dialog",className:"preview-dialog",
          params:{
            dom:this.pageview.refs.dailydesigner.getPreviewDom(),
            content:this.pageview.refs.dailydesigner.getConfig()
          }
      });
      },
      segment_itemclick:function(sender,param){
        sender.select();
        if(param.index===0){
          if(this.pageview.refs.dailydesigner.curSelectedItem){
            this._designerItemSelected();
          }else{
            this.pageview.refs.segment_statusview.changeStatus("noctl_wrap");
          }
        }else{
          this.pageview.refs.segment_statusview.changeStatus("template_wrap");
        }
      },
      segment_didmount:function(sender){
        sender.components[1].select();
      },
      _designerItemSelected:function(){
        var _this = this;
        var curSelectedItem =  this.pageview.refs.dailydesigner.curSelectedItem;
        var type = curSelectedItem.itemType;
        var ctl_wrap =type+"_wrap";
        var itemData = curSelectedItem.itemData;
        this.pageview.refs.segment_statusview.changeStatus(ctl_wrap);

        this.pageview.delegate(ctl_wrap,function(target){
          window.setTimeout(function(){
            target.setDisabled(_this.isDisabled,true);
          },500);
        });

        this.pageview.delegate(type+"_title_input",function(target){
          target.setValue(itemData.title);
        });

        if(type==="InputText"||type==="InputTextNum"){
          this.pageview.delegate(type+"_placeholder_textarea",function(target){
            target.setValue(itemData.description);
          });
        }

        if(type==="InputTextNum"){
          this.pageview.delegate("unit_label_input",function(target){
            target.setValue(itemData.unit);
          });
        }
        if(type==="InputCheckbox"||type==="InputRadio"){
           this.pageview.delegate(type+"_items_repeat",function(target){
            target.bindData(itemData.options);
          });
        }


        this.pageview.delegate(type+"_require_checkbox",function(target){
          target.setValue(itemData.isRequired==="1");
        });

      },
      InputRadio_items_repeat_add_icon_click:function(sender,params){
        var len = this.pageview.refs.InputRadio_items_repeat.datasource.length;
        if(len>=10){
          return;
        }
        if(this.isDisabled){
          return;
        }
        this.pageview.refs.InputRadio_items_repeat.addItem({string:"选项"});

        this.pageview.refs.dailydesigner.curSelectedItem.setOptions(this.pageview.refs.InputRadio_items_repeat.datasource);
      },
      InputRadio_items_repeat_item_del_click:function(sender,params){
        var len = sender.rowInstance.parent.components.length;
        if(len<=2){
          return;
        }
        sender.rowInstance.remove();
        this.pageview.refs.dailydesigner.curSelectedItem.setOptions(sender.rowInstance.parent.datasource);
      },
      InputRadio_items_repeat_item_input_keyup:function(sender,params){
        sender.datasource.string = sender.getValue();
        this.pageview.refs.dailydesigner.curSelectedItem.setOptions(this.pageview.refs.InputRadio_items_repeat.datasource);
      },


      InputCheckbox_items_repeat_item_del_click:function(sender,params){
        var len = sender.rowInstance.parent.components.length;
        if(len<=2){
          return;
        }
        sender.rowInstance.remove();
        this.pageview.refs.dailydesigner.curSelectedItem.setOptions(sender.rowInstance.parent.datasource);
      },
      InputCheckbox_items_repeat_item_input_keyup:function(sender,params){
        sender.datasource.string = sender.getValue();
        this.pageview.refs.dailydesigner.curSelectedItem.setOptions(this.pageview.refs.InputCheckbox_items_repeat.datasource);
      },

      InputCheckbox_items_repeat_add_icon_click:function(sender,params){
        var len = this.pageview.refs.InputCheckbox_items_repeat.datasource.length;
        if(len>=10){
          return;
        }
        if(this.isDisabled){
          return;
        }
        this.pageview.refs.InputCheckbox_items_repeat.addItem({string:"选项"});

        this.pageview.refs.dailydesigner.curSelectedItem.setOptions(this.pageview.refs.InputCheckbox_items_repeat.datasource);
      },
      InputCheckbox_require_checkbox_change:function(sender,params){
        this.pageview.refs.dailydesigner.curSelectedItem.setReqiured(params.value);
        sender.setValue(params.value);
      },
      InputRadio_require_checkbox_change:function(sender,params){
        this.pageview.refs.dailydesigner.curSelectedItem.setReqiured(params.value);
        sender.setValue(params.value);
      },
      InputTextNum_require_checkbox_change:function(sender,params){
        this.pageview.refs.dailydesigner.curSelectedItem.setReqiured(params.value);
        sender.setValue(params.value);
      },
      InputText_require_checkbox_change:function(sender,params){
        this.pageview.refs.dailydesigner.curSelectedItem.setReqiured(params.value);
        sender.setValue(params.value);
      },
      InputDate_require_checkbox_change:function(sender,params){
        this.pageview.refs.dailydesigner.curSelectedItem.setReqiured(params.value);
        sender.setValue(params.value);
      },

      InputTextNum_placeholder_textarea_keyup:function(sender,params){
        var description = sender.getValue();
        this.pageview.refs.dailydesigner.curSelectedItem.setDescription(description);
      },
      InputText_placeholder_textarea_keyup:function(sender,params){
        var description = sender.getValue();
        this.pageview.refs.dailydesigner.curSelectedItem.setDescription(description);
      },


     InputText_title_input_keyup:function(sender,params){
      var title = sender.getValue();
        this.pageview.refs.dailydesigner.curSelectedItem.setTitle(title);
     },
     unit_label_input_keyup:function(sender,paras){
       var title = sender.getValue();
         this.pageview.refs.dailydesigner.curSelectedItem.setUnit(title);
     },

     InputDate_title_input_keyup:function(sender,params){
      var title = sender.getValue();
        this.pageview.refs.dailydesigner.curSelectedItem.setTitle(title);
     },

     InputCheckbox_title_input_keyup:function(sender,params){
      var title = sender.getValue();
        this.pageview.refs.dailydesigner.curSelectedItem.setTitle(title);
     },

     InputRadio_title_input_keyup:function(sender,params){
      var title = sender.getValue();
        this.pageview.refs.dailydesigner.curSelectedItem.setTitle(title);
     },

     InputTextNum_title_input_keyup:function(sender,params){
      var title = sender.getValue();
        this.pageview.refs.dailydesigner.curSelectedItem.setTitle(title);
     },
     title_click:function(){
       this.pageview.goBack();
     },
     save_success_dialog_label_init:function(sender){
       this.save_success_dialog_label = sender;
       sender.config.text=this.save_success_text;
       // this.save_success_dialog_label.setText(this.save_success_text);
     },
     save_success_dialog_cancelbtn_click:function(){
       this.pageview.hideDialog("save_success_dialog");
     },
     save_success_dialog_okbtn_click:function(){
       this.pageview.hideDialog("save_success_dialog");
       this.pageview.goBack();
     },
  };
    return pageLogic;
});
